import earthData from '../earthData'
import { DrawBasePoint, ColorArray } from '@/utils/earth'


function loginPage() {
    this.modelList = []
    this.timer = null
}

loginPage.prototype.init = function () {
    let that = this
    // 视角设置在中国  126.53812341715674,45.74001665321582
    let position = new Cesium.Cartesian3.fromDegrees(126.5381234, 45.740016, 5000)
    earthData.earth._viewer.camera.setView({
        destination: position,
        // orientation: {
        //     heading: Cesium.Math.toRadians(-90),
        //     pitch: Cesium.Math.toRadians(-135),
        //     roll: Cesium.Math.toRadians(90),
        // }
    })

    // var viewer = new Cesium.Viewer("cesiumContainer");
    // 添加3D Tiles
    const tileSetPromise = new Cesium.Cesium3DTileset({
        url: "http://192.168.2.16:8090/tileset.json",
        maximumLevel: 10,
        skipLevelOfDetail: true,
        baseScreenSpaceError: 1024,
        maximumScreenSpaceError: 256, // 数值加大，能让最终成像变模糊
        skipScreenSpaceErrorFactor: 16,
        skipLevels: 1,
        immediatelyLoadDesiredLevelOfDetail: false,
        loadSiblings: true, // 如果为true则不会在已加载完概况房屋后，自动从中心开始超清化房屋
        cullWithChildrenBounds: true,
        cullRequestsWhileMoving: true,
        cullRequestsWhileMovingMultiplier: 10, // 值越小能够更快的剔除
        preloadWhenHidden: true,
        preferLeaves: true,
        maximumMemoryUsage: 128, // 内存分配变小有利于倾斜摄影数据回收，提升性能体验
        progressiveResolutionHeightFraction: 0.5, // 数值偏于0能够让初始加载变得模糊
        dynamicScreenSpaceErrorDensity: 0.1, // 数值加大，能让周边加载变快
        dynamicScreenSpaceErrorFactor: 1, // 不知道起了什么作用没，反正放着吧先
        dynamicScreenSpaceError: true // 根据测试，有了这个后，会在真正的全屏加载完之后才清晰化房屋
    })

    tileSetPromise.readyPromise.then(tileSet => {
        earthData.earth._viewer.scene.primitives.add(tileSet);
        changeHeight(tileSet, 310); // 将此 tileSet 提高 310 米
    });


    // 可通过此函数，来修改 tileSet 的高度
    function changeHeight(tileSet, height) {
        height = Number(height);
        if (isNaN(height)) { return; }
        const cartographic = Cesium.Cartographic.fromCartesian(tileSet.boundingSphere.center);
        const surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height);
        const offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, height);
        const translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
        tileSet.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
    }

    // let basePoint = new DrawBasePoint({
    //     earth: earthData.earth,
    //     position: [ 108.200384, 26.541969, 0 ],
    //     color: ColorArray('#ff0000'),
    //     scale: [30000, 30000, 500]
    // })
    // this.modelList.push(basePoint)

    // setTimeout(ani, 0)
    // function ani () {
    //     earthData.earth.camera.position[0] -= 0.0002
    //     that.timer = requestAnimationFrame(ani)
    // }
}

// 销毁本模块添加的全部模型与绑定数据
loginPage.prototype.modelDestroy = function () {
    // 清楚计时器
    if (this.timer) {
        cancelAnimationFrame(this.timer)
    }
    // 销毁全部模型实例
    this.modelList.forEach(item => {
        item.destroy()
    })
    this.modelList = []
}

export default loginPage